package com.java110.mall.cart.cmd.storeOrder;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.intf.cart.IStoreOrderCartInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

@Java110Cmd(serviceCode = "storeOrder.queryStoreOrder")
public class QueryStoreOrderCmd extends Cmd {

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;


    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {

        StoreOrderDto storeOrderDto = BeanConvertUtil.covertBean(reqJson, StoreOrderDto.class);
        String states = reqJson.getString("states");
        storeOrderDto.setStates(null);
        if(!StringUtil.isEmpty(states)){
            storeOrderDto.setStates(states.split(","));
        }

        int count = storeOrderInnerServiceSMOImpl.queryStoreOrdersCount(storeOrderDto);

        List<StoreOrderDto> storeOrderDtos = null;
        if (count > 0) {
            storeOrderDtos = storeOrderInnerServiceSMOImpl.queryStoreOrders(storeOrderDto);
        } else {
            storeOrderDtos = new ArrayList<>();
        }

        // 查询订单 商品
        queryStoreOrderCarts(storeOrderDtos);

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeOrderDto.getRow()), count, storeOrderDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        cmdDataFlowContext.setResponseEntity(responseEntity);

    }

    /**
     * 查询订单商品
     * @param storeOrderDtos
     */
    private void queryStoreOrderCarts(List<StoreOrderDto> storeOrderDtos) {
        if(ListUtil.isNull(storeOrderDtos)){
            return;
        }

        List<String> orderIds = new ArrayList<>();
        for(StoreOrderDto storeOrderDto: storeOrderDtos){
            orderIds.add(storeOrderDto.getOrderId());
        }

        StoreOrderCartDto storeOrderCartDto = new StoreOrderCartDto();
        storeOrderCartDto.setOrderIds(orderIds.toArray(new String[orderIds.size()]));
        List<StoreOrderCartDto> storeOrderCartDtos =
                storeOrderCartInnerServiceSMOImpl.queryStoreOrderCarts(storeOrderCartDto);
        if(ListUtil.isNull(storeOrderCartDtos)){
            return;
        }
        List<StoreOrderCartDto> tmpCarts = null;
        for(StoreOrderDto storeOrderDto: storeOrderDtos) {
            tmpCarts = new ArrayList<>();
            for (StoreOrderCartDto cartDto : storeOrderCartDtos) {
                if (!storeOrderDto.getOrderId().equals(cartDto.getOrderId())){
                    continue;
                }
                storeOrderDto.setCartType(cartDto.getCartType());
                tmpCarts.add(cartDto);
            }
            storeOrderDto.setStoreOrderCartDtos(tmpCarts);
        }
    }

}
